﻿#requires -runasadministrator
<# 
.SYNOPSIS
SILA : Shibboleth IdP Loader For ADFS

.DESCRIPTION
SILA is a script that I wanted to be simple for an easy adaptation that allows and easy loading of 
Shibboleth federation metadata into adfs.
SILA can also handles the refresh of the RHD page, but you should adapt this part to your need. 

For installation and more refers to http://sila.codeplex.com

(c) CNRS - 2012
licence : CeCiLL B - GPL v2

.NOTES
# 
# jmt-01oct2012 : create
# jmt-08nov2012-0.1 : add comment for release... It is still a working job
# jmt-10/11/12-0.2 : check validUntil value
# jmt-15/11/12-0.03 : check metdata signature
# jmt-08aug13-0.4 : reset acl for each file on the web server
# jmt-08aug13-0.5 : add copy of metadatafile to the server of the farm
# jmt-08aug13-0.6 : copy RHD file to servers with a backup
# jmt-23oct13-0.7 : split the initial script to have all configuration in the original file.
#                   SILA.ps1 = configuration file and entry point
#                   SILAmain.ps1 = main program
#                   SILAhelper.ps1 = helper function
# jmt-15nov13-0.8 : Generate JSON disco service file
jmt-25may14-0.9
#
# toDo
# error catching in trace File
#> 

#
# region parameters
#

$federationName = "RENATER"
# $federationName is used to create folder for each fedaration on the localweb server

$federationMetadataURL = "https://services-federation.renater.fr/metadata/renater-metadata.xml"
# the url of the federation metadata

$webServerURL = "https://adfs.srv.core-cloud.net/federation/"
# the url of the local web server, the one hosting ADFS. https protocol is mandatory

$serverNames = @("\\cnadf1wvp","\\cnadf2wvp")
# the names of the other servers in the ADFS array, the local server is managed direct

$metadataBaseFolder = "C$\inetpub\Federation"
# path to the folder where each IdP metadata will written. Using server syntax \\server\c$\inetpub\federation

$rhdBaseFolder = "C$\inetpub\adfs\ls"
# path to the folder where each IdP metadata will written. Using server syntax \\server\c$\inetpub\federation

$scriptFolder = "C:\Users\adminthia\Desktop\SILA"

$rhdTemplateName = "HomeRealmDiscovery.aspx"
# the template for the RHD page

$rhdTemplate = Join-Path $scriptFolder "HomeRealmDiscovery.aspx"
# the template for the RHD page

$outPath = Join-Path $scriptFolder "temp\"
# temp folder

$ruleFilePath = Join-Path $scriptFolder "ClaimProviderRules.txt"
# ADFS rules that will be add to each trusted claim provider (IdP)

$discoJsonFile = Join-Path $outPath "disco.json"
# the Json discovery service file

$generateDiscoJson = $false
# set to true if discovery service should be done

$generateRHD = $False
# Generate the RHD page and replace it on ADFS servers

$adfsManualIdp = @(
    "AD AUTHORITY", # this value must remain
	"urn:mace:cnrs.fr:janus.dsi.cnrs.fr",
    "urn:mace:cru.fr:federation:sac"
)
# manually added IdP that are not in the federation metadata. Those won't be remove from from the Claims Provider Trusts. Do not remove AD AUTHORITY 

#
# Log and report
#

$logPath = Join-Path $scriptFolder "Log\"
# every run of the script are traced in this folder

$logSuffix = "_sila.log"
# log file naming is : yyyy-MM-dd_HHmmss_sila.log 

# logging parameter
$MailReportSmtpHost="172.22.253.17"
$MailReportFrom = "SILA Batch <noReply@dsi.cnrs.fr>"
$MailTo = "Jean Marie THIA <jean-marie.thia@dsi.cnrs.fr>"
$MailBody = "SILA batch report"
$MailSubject = "SILA Batch Report"


# load helper functions
cd $scriptFolder
. .\SILAhelper.ps1
. .\SILAmain.ps1

if ($generateDiscoJson) 
{
	. .\SILAdiscoJson.ps1
}

# launch SILA script
main